package com.ds.home.comet;

import com.ds.common.JDSException;
import com.ds.context.JDSActionContext;
import com.ds.context.JDSCometContext;
import com.ds.context.JDSContext;
import com.ds.engine.ConnectInfo;
import com.ds.engine.ConnectionHandle;
import com.ds.engine.JDSSessionHandle;
import com.ds.home.client.GWClient;
import com.ds.home.engine.HomeServer;
import com.ds.home.udp.SendAppMsg;
import com.ds.msg.Msg;
import com.ds.msg.SensorMsg;
import com.ds.server.JDSClientService;
import com.ds.server.JDSServer;
import com.ds.server.comet.AbstractCometHandle;
import com.ds.server.eumus.ConfigCode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;
import java.util.concurrent.Executors;

public class GHCometHandle extends AbstractCometHandle {

    public GHCometHandle(JDSClientService client, JDSSessionHandle sessionHandle, String systemCode) throws JDSException {
        super(client, sessionHandle, systemCode);

    }

    public void receive(String receiveStr) throws JDSException {
        logger.info("receive user:[" + getConnectInfo().getLoginName() + "] ");
        logger.info(receiveStr);
    }

    public boolean send(String msgString) throws JDSException {
        logger.info(msgString);
        return super.send(msgString);
    }

    class ClearSession implements Runnable {
        private ConnectionHandle sessionHandle;
        private ConnectInfo connectInfo;
        private String systemCode;

        ClearSession(ConnectInfo connectInfo, ConnectionHandle handle, String systemCode) {
            this.sessionHandle = handle;
            this.connectInfo = connectInfo;
            this.systemCode = systemCode;
        }

        public void run() {
            try {
                Set<JDSSessionHandle> sessionHandleList = JDSServer.getInstance().getSessionHandleList(connectInfo);
                for (JDSSessionHandle handle : sessionHandleList) {
                    if (!handle.toString().equals(sessionHandle.toString())) {
                        JDSClientService client = JDSServer.getInstance().getJDSClientService(handle, ConfigCode.app);
                        if (client != null && client.getConnectInfo() != null) {
                            client.getConnectionHandle().disconnect();
                        }
                    }

                }
            } catch (JDSException e) {
                e.printStackTrace();
            }

        }

    }

    public void connect(JDSContext context) throws JDSException

    {

        if (context instanceof JDSCometContext) {
            JDSCometContext cometContext = (JDSCometContext) context;
            this.request = (HttpServletRequest) cometContext.getHttpRequest();
            this.response = (HttpServletResponse) cometContext.getHttpResponse();
        } else {
            throw new JDSException("用户未登录！", JDSException.PROCESSDEFINITIONERROR);
        }

        if (this.connectInfo == null) {
            connectInfo = this.getClient().getConnectInfo();
        }

        if (this.connectInfo == null) {
            throw new JDSException("用户未登录！", JDSException.PROCESSDEFINITIONERROR);
        }

        Set<JDSSessionHandle> sessionHandleList = JDSServer.getInstance().getSessionHandleList(connectInfo);
        for (JDSSessionHandle handle : sessionHandleList) {
            if (!handle.toString().equals(client.getSessionHandle().toString())) {
                JDSClientService client = JDSServer.getInstance().getJDSClientService(handle, ConfigCode.app);
                if (client != null && client.getConnectInfo() != null) {
                    client.getConnectionHandle().disconnect();
                }
            }

        }
        client.connect(connectInfo);
        GWClient gwclient = HomeServer.getInstance().getGWClient(client);
        gwclient.gatewayOnLine(connectInfo.getLoginName());
        gwclient.connect(connectInfo);
        String gatewayieee = gwclient.getDeviceById(connectInfo.getLoginName()).getSerialno();
        super.connect(context);

    }

    public void disconnect() throws JDSException {

        super.disconnect();
    }

    @Override
    public boolean repeatMsg(Msg msg, JDSSessionHandle handle) throws JDSException {
        SendAppMsg msgRun = new SendAppMsg((SensorMsg) msg, handle, JDSActionContext.getActionContext());
        Executors.newSingleThreadExecutor().execute(msgRun);
        return true;
    }
}
